<?php
class SkuController extends Zend_Controller_Action
{	
	public function init(){
		IndexController::initAdmin($this);
	}
	
	public function indexAction()
	{
		CCommon::clear_chart_cache($this->AppUI->country);
		exit;
	}
	
	public function popupAction()
	{
		$strCallback	= $this->_request->getParam('callback', null);
		$strSku			= $this->_request->getParam('sku', null);
		$strCategory	= $this->_request->getParam('c', null);
	
		if($strCallback && $strSku)
		{
			$strFilename	= ROOT_SITE."/static/chart/". $this->AppUI->country . "/" .$strSku.".png";
			
			if( !file_exists($strFilename) )
			{

				#Chart
				$objCategory	= new CCategory($this->AppUI->country);
				$arrData		= $objCategory->get();
				$arrCategory	= isset($arrData['category']) ? $arrData['category'] : null;
				$arrCateItem	= array();
				foreach($arrCategory as $category)
				{
					if( strtolower(trim($category['name']))==strtolower(trim($strCategory)) ){
						$category['name'] = trim($category['name']); 
						$arrCateItem	= $category;
						break;
					}
				}
				
				$strDateFrom	= isset($arrCateItem['s_df']) ? $arrCateItem['s_df'] : null;
				$strDateTo		= isset($arrCateItem['s_dt']) ? $arrCateItem['s_dt'] : null;
				
				$arrParams	= array(
					'orf'	=> 'date',
					'orv'	=> 'asc'
				);
				$obj		= new QSale($this->AppUI->country);
				
				$arrData	= $obj->get_item_analytic($strSku, $strDateFrom, $strDateTo, $arrParams);
							
				if($arrData)
				{
					$arrResultD	= array();
					foreach($arrData as $data)
					{
						if( !isset($arrResultD[$data['date']]) ){
							$arrResultD[$data['date']]	= 0;
						}
						$arrResultD[$data['date']]++;
					}
				
					$intTimeFrom	= CCommon::mkTimeDate($strDateFrom);
					$intTimeTo		= CCommon::mkTimeDate($strDateTo);

				
					$index			= $intTimeFrom;
					while( $index<=$intTimeTo )
					{
						$date		= date('Y-m-d', $index);
						if( !isset($arrResultD[$date]) ){
							$arrResultD[$date]	= 0;
						}
						$arrResult[$date]	= $arrResultD[$date];
						$index				= $index + (60*60*24);
					}
					
					$arrX	= array();
					$arrY	= array();
					$month	= 0;
					$arrMonth	= array('Jan','Feb','Mar','Arp','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
					
					foreach($arrResult as $key=>$value)
					{
						$arrTemp	= explode('-', $key);
						if($arrTemp[1]!=$month)
						{
							$month		= $arrTemp[1];
							#$arrX[]	= '['.$month.'-'.$arrTemp[2].']';
							$arrX[]		= $arrMonth[$month-1].'-'.$arrTemp[2];
						}
						else{
							$arrX[]		= $arrTemp[2];
						}
						
						$arrY[]		= $value==null? 0: intval($value);
					}

					$this->_draw_chart($strFilename, $strSku, $arrX, $arrY);
				}
				#Chart
			}
		
			$strFilename	= str_replace(ROOT_SITE, HOST, $strFilename);
			$this->view->strChart_filename	= $strFilename;;
			$this->view->mixtime			= time();
			
			$arrResult	= array('html'	=> $this->view->fetch('sku/popup.tpl'));
			echo $strCallback.'('.json_encode($arrResult).')';
			
		}
		exit;
	}
	
	private function _draw_chart($p_strFilename, $p_strSku, $p_arrDataNameX, $p_arrDataY)
	{
		include("PChart/pChart/pData.class");
		include("PChart/pChart/pChart.class");
		// Dataset definition 
		$DataSet = new pData;
		$arrY = isset($p_arrDataY) ? $p_arrDataY:0;
		$arrX = isset($p_arrDataNameX) ? $p_arrDataNameX:0;
		$DataSet->AddPoint($arrY, "Serie1", "", $arrX);
		
		$DataSet->AddAllSeries();
		$DataSet->SetAbsciseLabelSerie();
		$DataSet->SetSerieName("Item","Serie1");

		$Test = new pChart(1000,330);
		$Test->setFontProperties(ROOT_SITE."/library/PChart/Fonts/tahoma.ttf",8);
		$Test->setGraphArea(50,30,950,300);
		$Test->drawFilledRoundedRectangle(7,7,993,323,5,240,240,240);
		$Test->drawRoundedRectangle(5,5,995,325,5,230,230,230);
		$Test->drawGraphArea(255,255,255,TRUE);
		$Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2,TRUE);
		$Test->drawGrid(4,TRUE,230,230,230,50);

		// Draw the 0 line
		$Test->setFontProperties(ROOT_SITE."/library/PChart/Fonts/tahoma.ttf",6);
		$Test->drawTreshold(0,143,55,72,TRUE,TRUE);

		// Draw the bar graph
		$Test->drawOverlayBarGraph($DataSet->GetData(),$DataSet->GetDataDescription());

		// Finish the graph
		$Test->setFontProperties(ROOT_SITE."/library/PChart/Fonts/tahoma.ttf",8);
		$Test->drawLegend(930,10,$DataSet->GetDataDescription(),255,255,255);
		$Test->setFontProperties(ROOT_SITE."/library/PChart/Fonts/tahoma.ttf",12);
		$Test->drawTitle(50,22,$p_strSku,50,50,50,885);
		$Test->Render($p_strFilename);
	}
}
?>
